package com.jerry.cui.dingtalk.service;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiV2DepartmentCreateRequest;
import com.dingtalk.api.request.OapiV2DepartmentGetRequest;
import com.dingtalk.api.request.OapiV2DepartmentListparentbyuserRequest;
import com.dingtalk.api.request.OapiV2DepartmentListsubRequest;
import com.dingtalk.api.response.OapiV2DepartmentCreateResponse;
import com.dingtalk.api.response.OapiV2DepartmentGetResponse;
import com.dingtalk.api.response.OapiV2DepartmentListparentbyuserResponse;
import com.dingtalk.api.response.OapiV2DepartmentListsubResponse;
import com.jerry.cui.dingtalk.oauth2.DingtalkOAuth2AccessToken;
import com.jerry.cui.dingtalk.properties.DingtalkProperties;
import com.taobao.api.ApiException;

@Component
public class DingTalkDepartmentService {

    @Autowired
    private DingtalkOAuth2AccessToken dingtalkOAuth2AccessToken;
    @Autowired
    private DingtalkProperties dingtalkProperties;

    /**
     * 查询子部门列表
     * @throws ApiException
     */
    public OapiV2DepartmentListsubResponse getDepartmentSubList(Long deptId, String language) throws ApiException {
        DingTalkClient client = new DefaultDingTalkClient(dingtalkProperties.getApi().getDepartment_listsub());
        OapiV2DepartmentListsubRequest request = new OapiV2DepartmentListsubRequest();
        if(null != deptId){
        	request.setDeptId(deptId);
        }
        if(StringUtils.isNotEmpty(language)){
        	request.setLanguage(language);
        }
        request.setHttpMethod("GET");
        return client.execute(request, dingtalkOAuth2AccessToken.getAccessToken());
    }

    /**
     * 获取部门详情
     * @throws ApiException
     */
    public OapiV2DepartmentGetResponse getDepartment(Long deptId, String language) throws ApiException {
        Assert.isTrue(deptId!=null,"部门ID未设定");
        DingTalkClient client = new DefaultDingTalkClient(dingtalkProperties.getApi().getDepartment_get());
        OapiV2DepartmentGetRequest request = new OapiV2DepartmentGetRequest();
        request.setDeptId(deptId);
        request.setHttpMethod("GET");
        OapiV2DepartmentGetResponse response = client.execute(request, dingtalkOAuth2AccessToken.getAccessToken());
        return response;
    }

    /**
     * 新建部门
     */
    public OapiV2DepartmentCreateResponse createDepartment(OapiV2DepartmentCreateRequest request) throws ApiException {
        DingTalkClient client = new DefaultDingTalkClient(dingtalkProperties.getApi().getDepartment_create());
        if(request.getParentId()==null){
            request.setParentId(1L);
        }
        return client.execute(request, dingtalkOAuth2AccessToken.getAccessToken());
    }
    	
    /**
     * 获取指定用户的所有父部门列表
     */
    public OapiV2DepartmentListparentbyuserResponse listparentbyuser(String userid) throws ApiException {
    	DingTalkClient client = new DefaultDingTalkClient(dingtalkProperties.getApi().getDepartment_listparentbyuser());
    	Assert.isTrue(userid!=null,"用户ID未设定");
    	OapiV2DepartmentListparentbyuserRequest request = new OapiV2DepartmentListparentbyuserRequest();
    	request.setUserid(userid);
    	return client.execute(request, dingtalkOAuth2AccessToken.getAccessToken());
    }

}
